package com.zhoust.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 15703
 * @version 1.0
 * @Title
 * @package com.zhoust.config
 * @date 2020/10/20 16:35
 * @desc 如果不是集群 排除文件中 com.zhoust.config.RedisAutoConfiguration 文件配置
 */

//@Configuration
public class RedisAutoConfiguration {

    @Value("#{'${spring.redis.clusterNodes}'.split(',')}")
    private List<String> clusterIpPortList;
    @Value("${spring.redis.connectionTimeout}")
    private int connectionTimeout;
    @Value("${spring.redis.soTimeout}")
    private int soTimeout;
    @Value("${spring.redis.maxAttempts}")
    private int maxAttempts;
    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public JedisCluster jedisCluster() {
        Set<HostAndPort> nodes = new HashSet<>();
        for (String node : clusterIpPortList){
            String [] ipPortStr = node.split(":");
            nodes.add(new HostAndPort(ipPortStr[0].trim(), Integer.valueOf(ipPortStr[1].trim())));
        }
        JedisCluster jedisCluster = new JedisCluster(nodes, connectionTimeout, soTimeout, maxAttempts,password , new GenericObjectPoolConfig());
        return jedisCluster;
    }
}
